﻿using BCAMap.AMap;
using BCAMap.AMap.Dto;
using BCCommon.TMS.Transport;
using BCData.TMS.VehicleCategoryReference;
using BCDto.MallInterface;
using BCDto.TMS.MallInterface;
using BCDto.TMS.VehicleType;
using BCService.TMS.MallInterface.OrderAsTo;
using BCService.TMS.TransportOrder;
using BCService.TMS.VehicleTypeService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCService.TMS.MallInterface
{
    /// <summary>
    /// 配送车型选择和运费计算
    /// </summary>
    public class VehicleSelectService : IVehicleSelectService
    {
        private const string vehicleCategory = "VehicleCategory_Box";
        private IVehicleTypeService vehicleTypeService;
        private IVehicleCategoryReferenceData vehicleCategoryReferenceData;
        private MallOrderResposeDto mallOrderResposeDto;
        private readonly IAMapUtils iAMapUtils;
        private readonly ITransportOrderService transportOrderService;


        public VehicleSelectService(IAMapUtils iAMapUtils, IVehicleTypeService vehicleTypeService, IVehicleCategoryReferenceData vehicleCategoryReferenceData, ITransportOrderService transportOrderService)
        {
            this.mallOrderResposeDto = new MallOrderResposeDto();

            this.iAMapUtils = iAMapUtils;
            this.vehicleTypeService = vehicleTypeService;
            this.vehicleCategoryReferenceData = vehicleCategoryReferenceData;
            this.transportOrderService = transportOrderService;
        }

        public MallOrderResposeDto GetResult(MallOrderDto mallOrderDto)
        {
            mallOrderResposeDto.TMSManageFeeBusinessType = BCCommon.TMS.ManageFeeBusiness.TMSManageFeeBusinessType.Transport;
            mallOrderResposeDto.GoodsTotalWeight = mallOrderDto.GetTotalGoodsWeight();
            mallOrderResposeDto.GoodsTotalVolume = mallOrderDto.GetTotalGoodsVolume();
            mallOrderResposeDto.IsBeginCalcCost = false; 
            mallOrderResposeDto.Result = false;
            mallOrderResposeDto.ResultDiscription = string.Empty;
            mallOrderResposeDto.Distance = 0;
            mallOrderResposeDto.vehicleTypeDto = null;

            return GetDistance(mallOrderDto);
        }

        #region 获取距离
        public DistanceResponseDto GetDistanceResponseDto(double lng1, double lat1, double lng2, double lat2)
        {
            DistanceRequestDto distanceRequestDto = new DistanceRequestDto();
            distanceRequestDto.Origins = new List<double[]>();

            var Origin = new double[2];
            distanceRequestDto.Destination = new double[2];
            Origin[0] = lng1;
            Origin[1] = lat1;
            distanceRequestDto.Origins.Add(Origin);
            distanceRequestDto.Destination[0] = lng2;
            distanceRequestDto.Destination[1] = lat2;
            DistanceResponseDto dto = iAMapUtils.GetDistance(distanceRequestDto);
            return dto;
        }


        private MallOrderResposeDto GetDistance(MallOrderDto mallOrderDto)
        {
            var dto = GetDistanceResponseDto(mallOrderDto.Sender.Lng, mallOrderDto.Sender.Lat, mallOrderDto.Receiver.Lng, mallOrderDto.Receiver.Lat);
            if (dto != null)
            {
                if (dto.Status == "1")
                {
                    mallOrderResposeDto.Distance = 0;
                    foreach (var item in dto.Results)
                    {
                        int len = 0;
                        if (int.TryParse(item.Distance, out len))
                            mallOrderResposeDto.Distance += len;
                    }
                    return GetVehicleTypeMoreGoods(mallOrderDto);
                }
                else
                {
                    mallOrderResposeDto.Result = false;
                    mallOrderResposeDto.ResultDiscription = $"获取高德距离失败,错误代码{dto.Info}!";
                    return mallOrderResposeDto;
                }
            }
            else
            {
                mallOrderResposeDto.Result = false;
                mallOrderResposeDto.ResultDiscription = $"获取高德距离无返回!";
                return mallOrderResposeDto;
            }
        }
        #endregion



        private MallOrderResposeDto GetVehicleTypeMoreGoods(MallOrderDto mallOrderDto)
        {
            MallOrderGoodsDto mallGoodsDto = mallOrderDto.GetMaxLengthGoods();
            decimal goodsVolumeCount = 0; 
            goodsVolumeCount = mallOrderDto.GetTotalGoodsVolume(); 
            var mallGoodsMaxWeight = mallOrderDto.GetTotalGoodsWeight();

            mallOrderResposeDto.Result = false;
            var vehicleTypeList = vehicleTypeService.VehicleNameList();
            if (vehicleTypeList != null && vehicleTypeList.Count > 0)
            {
                foreach (var item in vehicleTypeList.OrderBy(s => s.MaxWeight))
                {
                    if (item.GetLength() >= mallGoodsDto.GetLength() && item.GetWidth() > mallGoodsDto.GetWidth() && item.GetHight() > mallGoodsDto.GetHight())
                    {
                        if (item.MaxWeight > mallGoodsMaxWeight)
                        { 
                            decimal vehicleVolume = (decimal)(item.GetLength() * item.GetWidth() * item.GetHight());
                            if (vehicleVolume > goodsVolumeCount)
                            {
                                mallOrderResposeDto.IsBeginCalcCost = true;
                                var ret = GetVehicleCost(mallOrderDto, item);
                                return ret;
                                //if (ret.orderCostChecklistDtos.c > 0)
                                //{
                                //    mallOrderResposeDto.Result = true;
                                //    mallOrderResposeDto.vehicleTypeDto = item;
                                //    return mallOrderResposeDto;
                                //}
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                }
                mallOrderResposeDto.ResultDiscription = "找不到适合货物尺寸和重量的车辆!";
                return mallOrderResposeDto;
            }
            else
            {
                mallOrderResposeDto.Result = false;
                mallOrderResposeDto.ResultDiscription = "车型列表空!";
                return mallOrderResposeDto;
            }
        }
        private MallOrderResposeDto GetVehicleCost(MallOrderDto mallOrderDto, VehicleTypeDto vehicleTypeDto)
        {
            var orderService = transportOrderService.GetOrderService(TransportBusinessType.SmallPieces, null);
            MallOrderTransf mallOrderTransf = new MallOrderTransf();
            var orderShippingCostRequestDto = mallOrderTransf.TransfCastOrder(mallOrderDto);
            orderShippingCostRequestDto.TransportBusinessType = TransportBusinessType.SmallPieces;

            mallOrderResposeDto.orderCostChecklistDtos = orderService.CalculateShippingCost(null,orderShippingCostRequestDto);
            mallOrderResposeDto.Result = true;
            return mallOrderResposeDto;
        }
    }
}
